﻿@using System.Reflection
@using AdAggregator.Attribute
@using AdAggregator.Util
@using RealEstateAggregator.Models
@using RealEstateAggregator.Utils
@model RealEstateAggregator.Models.SearchData.MySearchData

@{
    ViewBag.Title = "Wyszukiwarka Nieruchomości";
}
<script type="text/javascript" src="~/Content/Scripts/pagination.js"></script>
<script>
    $(document).ready(function () {
        $("#toggleAdvancedSearch").click(function () {
            $(this).find("span.arrow").toggleClass('hidden');
            $("#advancedSearch").slideToggle();
        });
        pagination(@ViewBag.PageCount);
    });
</script>


@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <div class="page">
        <div id="search">
            <h2>Wyszukiwarka nieruchomości</h2>

            <div class="editor-label">
                @Html.LabelFor(model => model.Voivodship)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(model => model.Voivodship, Constants.Voivodships.Select((v, i) => new SelectListItem
                    {
                        Text = v,
                        Value = v
                    }))
                @Html.ValidationMessageFor(model => model.Voivodship)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.City)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.City)
                @Html.ValidationMessageFor(model => model.City)
            </div>

            <div class="clear"></div>

            <div class="editor-label">
                @Html.LabelFor(model => model.AdvertisementType)
            </div>
            <div class="editor-field">
                @Html.DropDownListForEnum(model => model.AdvertisementType, typeof(AdvertisementType))
                @Html.ValidationMessageFor(model => model.AdvertisementType)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.EstateType)
            </div>
            <div class="editor-field">
                @Html.DropDownListForEnum(model => model.EstateType, typeof(EstateType))
                @Html.ValidationMessageFor(model => model.EstateType)
            </div>

            <div class="clear"></div>

            <!-- ADVANCED SEARCH -->
            <div id="toggleAdvancedSearch"><span class="arrow hidden">▲</span><span class="arrow">▼</span>Szczegółowe opcje wyszukiwania</div>
            <div id="advancedSearch">
                <div class="editor-label">
                    @Html.LabelFor(model => model.MarketType)
                </div>
                <div class="editor-field">
                    @Html.DropDownListForEnum(model => model.MarketType, typeof(MarketType))
                    @Html.ValidationMessageFor(model => model.MarketType)
                </div>

                <div class="clear"></div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.District)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.District)
                    @Html.ValidationMessageFor(model => model.District)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Street)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Street)
                    @Html.ValidationMessageFor(model => model.Street)
                </div>

                <div class="clear"></div>

                <div class="editor-label">
                    Cena od - do
                </div>
                <div class="editor-field short-input">
                    @Html.EditorFor(model => model.PriceMin)
                    @Html.EditorFor(model => model.PriceMax)
                </div>

                <div class="editor-label">
                    Cena za metr od - do
                </div>
                <div class="editor-field short-input">
                    @Html.EditorFor(model => model.PricePerMeterMin)
                    @Html.EditorFor(model => model.PricePerMeterMax)
                </div>

                <div class="clear"></div>

                <div class="editor-label">
                    Powierzchnia od - do
                </div>
                <div class="editor-field short-input">
                    @Html.EditorFor(model => model.AreaMin)
                    @Html.EditorFor(model => model.AreaMax)
                </div>

                <div class="editor-label">
                    Liczba pokoi od - do
                </div>
                <div class="editor-field short-input">
                    @Html.TextBoxFor(model => model.RoomCountMin)
                    @Html.TextBoxFor(model => model.RoomCountMax)
                </div>
                <div class="clear"></div>
                <div class="editor-label">
                    Piętro od - do
                </div>
                <div class="editor-field short-input">
                    @Html.TextBoxFor(model => model.FloorMin)
                    @Html.TextBoxFor(model => model.FloorMax)
                </div>

                <div class="editor-label">
                    Rok budowy od - do
                </div>
                <div class="editor-field short-input">
                    @Html.TextBoxFor(model => model.YearBuiltMin)
                    @Html.TextBoxFor(model => model.YearBuiltMax)
                </div>

                <div class="clear"></div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Keywords)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Keywords)
                    @Html.ValidationMessageFor(model => model.Keywords)
                </div>

                <div class="clear"></div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.OnlyWithPhoto)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.OnlyWithPhoto)
                    @Html.ValidationMessageFor(model => model.OnlyWithPhoto)
                </div>

                <div class="clear"></div>
            </div>
            <p>
                <input type="submit" name="searchSubmit" value="Szukaj!" />
            </p>
        </div>
        @Html.DropDownListForSortableProperties(model => model.OrderBy, ViewBag.Props as IEnumerable<PropertyInfo>)
        @Html.DropDownListForEnum(model => model.SortingDirection, typeof(SortingDirection))
        @if (IsPost)
        {
            <div class="navigationButtons"></div>
            foreach (MyAdvertisement ad in ViewBag.Results)
            {
            <div class="ad">
                <div class="title">
                    <a href="@ad.Link" target="_blank">@ad.Title</a>
                    <div class="siteLogo">
                        <img src="@Url.Content("~/Content/Img/" + ad.AdSource)" alt=""/>
                    </div>
                </div>
                <div>
                    <div class="preview">
                        <img src="@ad.Photo" height="150" alt="@ad.Title"/>
                    </div>
                    <div class="desc">
                        <table class="detailsTable">
                            <tr>
                                <td>Lokalizacja:</td>
                                <td colspan="3">@ad.Location</td>
                            </tr>
                            <tr>
                                <td>Cena:</td>
                                <td>@ad.Price @if (ad.Price.HasValue)
                                              { @Constants.Zloty }</td>
                                <td>Cena/m<sup>2</sup>:</td>
                                <td>@ad.PricePerMeter  @if (ad.PricePerMeter.HasValue)
                                                       { @Constants.Zloty }</td>
                            </tr>
                            <tr>
                                <td>Powierzchnia:</td>
                                <td>@ad.Area @if (ad.Area.HasValue)
                                             { @Constants.SquareMeter }</td>
                                <td>Piętro:</td>
                                <td>@ad.Floor</td>
                            </tr>
                            <tr>
                                <td>Pokoje:</td>
                                <td>@ad.RoomCount</td>
                                <td>Rok budowy:</td>
                                <td>@ad.YearBuilt</td>
                            </tr>
                        </table>
                    </div>
                    <div class="more">
                        <a href="@ad.Link" target="_blank">Pełne ogłoszenie &raquo;</a>
                    </div>
                    <div class="date">
                        @if (ad.Modified.HasValue)
                        {
                            @ad.Modified.Value.ToString("dd-MM-yyyy")
                        }
                    </div>
                    <div class="clear"></div>
                </div>
            </div>
            }

            <div class="navigationButtons"></div>
            @Html.HiddenFor(model => model.Page)
        }
    </div>
}

